package com.tbsoft.ehr.action.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.login.MenuInfo;
import com.tbsoft.ehr.login.RoleInfo;
import com.tbsoft.ehr.service.user.RoleService;
/**
 * @desc 角色维护
 * @author vidy
 *
 */
public class RoleAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private RoleService roleService;
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	private Log log = LogFactory.getLog(this.getClass());
	
	private RoleInfo role;
	
	private List<RoleInfo> listRole = new ArrayList<RoleInfo>();
	private List<MenuInfo> menuList;

	public List<MenuInfo> getMenuList() {
		return menuList;
	}

	public List<RoleInfo> getListRole() {
		return listRole;
	}

	public void setListRole(List<RoleInfo> listRole) {
		this.listRole = listRole;
	}

	public RoleInfo getRole() {
		return role;
	}

	public void setRole(RoleInfo role) {
		this.role = role;
	}
	public String list(){
		listRole.clear();
		int pageNum = this.getPageNum() > 0 ? this.getPageNum() - 1 : 0;
		int startIndex = pageNum * getNumPerPage();
		try{
			List<RoleInfo> tmpList = roleService.getRoleList(role, startIndex, getNumPerPage());
			listRole.addAll(tmpList);
		}catch(Exception e){
			log.error("list exception", e);
			setStatusCode("300");
			setMessage(e.getMessage());
		}
		return "browse";
	}
	public String save(){
		try{
			roleService.saveRole(role);
		}catch(Exception e){
			log.error("save exception", e);
			setStatusCode("300");
			setMessage("新增角色错误，请确认角色Id是否已存在。");
		}
		setCallbackType("closeCurrent");
		setRel("role_browse");
		return SUCCESS;
	}
	public String delete(){
		try{
			roleService.deleteRole(role);
		}catch(Exception e){
			log.error("delete exception", e);
			setStatusCode("300");
			setMessage("删除角色失败.");
		}
		setRel("role_browse");
		return SUCCESS;
	}
	public String openMenu(){
		role = roleService.getRoleById(role.getRoleId());
		menuList = roleService.getAllMenuList();
		for(MenuInfo smi:role.getMenuSet()){
			for(MenuInfo rmi:menuList){
				for(MenuInfo cmi:rmi.getChildMenuInfo()){
					if(smi.getMenuId().equals(cmi.getMenuId())){
						cmi.setSelect(true);
					}
				}
			}
		}
		return "roleMenu";
	}
	private List<String> menuCheckList = new ArrayList<String>();
	public List<String> getMenuCheckList() {
		return menuCheckList;
	}

	public void setMenuCheckList(List<String> menuCheckList) {
		this.menuCheckList = menuCheckList;
	}
	public String menu(){
		try{
			roleService.saveMenuList(role, menuCheckList);
		}catch(Exception e){
			log.error("menu exception", e);
			setStatusCode("300");
			setMessage("菜单授权失败.");
		}
		setRel("role_browse");
		return SUCCESS;
	}
	
	

}
